Ext.require(['Ext.util.History'])
Ext.define('ProjectAON.controller.DataGrid', {
    extend:'Ext.app.Controller',

    requires:['ProjectAON.ux.ActivityMonitor',
        'Ext.window.MessageBox'],

    stores:['BeheerLeraar',
        'BeheerLeerling',
        'Vak',
        'Landen',
        'Functie'],
    models:['BeheerLeraar',
        'BeheerLeerling',
        'Vak',
        'Landen',
        'Functie'],
    views:[
        'lerarenBeheren.Scherm29',
        'vakkenBeheren.Scherm45',
        'leerlingenBeheren.Scherm31',
        'Viewport'
    ],

    isWidgetOpen:false,

    store:null,

    list:null,

    addwidget:null,

    editwidget:null,

    init:function () {
        this.control({
            'scherm29 button[action=edit]':{
                click:this.showEditWidget
            },
            'scherm29 button[action=add]':{
                click:this.showAddWidget
            },
            'scherm29 button[action=remove]':{
                click:this.showRemoveUser
            },
            'scherm45 button[action=edit]':{
                click:this.showEditWidget
            },
            'scherm45 button[action=add]':{
                click:this.showAddWidget
            },
            'scherm45 button[action=remove]':{
                click:this.showRemoveUser
            },
            'scherm45':{
                afterrender:this.vakGridActive
            },
            'useredit button[action=save]':{
                click:this.updateUser
            },
            'useradd button[action=save]':{
                click:this.insertUser
            },
            'useredit button[action=close]':{
                click:this.setWidgetClosed
            },
            'useradd button[action=close]':{
                click:this.setWidgetClosed
            },
            'searchgrid':{
                itemdblclick:this.showEditWidget
            },
            'useredit':{
                destroy:this.setWidgetClosed
            },
            'useradd':{
                destroy:this.setWidgetClosed
            },
            'scherm31 button[action=edit]':{
                click:this.showEditWidget
            },
            'scherm31 button[action=add]':{
                click:this.showAddWidget
            },
            'scherm31 button[action=remove]':{
                click:this.showRemoveUser
            },
            'studentedit button[action=save]':{
                click:this.updateUser
            },
            'studentadd button[action=save]':{
                click:this.insertUser
            },
            'studentedit button[action=close]':{
                click:this.setWidgetClosed
            },
            'studentadd button[action=close]':{
                click:this.setWidgetClosed
            },
            'studentedit':{
                destroy:this.setWidgetClosed
            },
            'studentadd':{
                destroy:this.setWidgetClosed
            },
            'scherm29':{
                afterrender:this.teacherGridActive
            },
            'scherm31':{
                afterrender:this.studentGridActive
            },
            'vakedit button[action=save]':{
                click:this.updateUser
            },
            'vakadd button[action=save]':{
                click:this.insertUser
            },
            'vakedit button[action=close]':{
                click:this.setWidgetClosed
            },
            'vakadd button[action=close]':{
                click:this.setWidgetClosed
            },
            'vakedit':{
                destroy:this.setWidgetClosed
            },
            'vakadd':{
                destroy:this.setWidgetClosed
            }
        });
    },

    updateUser:function (button) {
        var win = button.up('window'),
            form = win.down('form'),
            record = form.getRecord(),
            values = form.getValues();
        console.log(values);
        record.set(values);

        this.store.sync();
        win.close();
        this.isWidgetOpen = false;
    },

    insertUser:function (button) {
        var win = button.up('window'),
            form = win.down('form'),
            values = form.getValues();

        this.store.add(values);
        this.store.sync();

        win.close();
        this.isWidgetOpen = false;
    },

    removeUser:function (selection) {
        this.store.remove(selection);
        this.store.sync();
    },

    showMessageBox:function (title, text) {
        Ext.MessageBox.show({
            title:title,
            msg:text,
            buttons:Ext.MessageBox.OK,
            icon:Ext.MessageBox.INFO,
            minWidth:400,
            maxWidth:600
        });
    },

    showEditWidget:function () {
        if (!this.isWidgetOpen) {
            var list = Ext.ComponentQuery.query(this.list)[0];

            var selection = list.getSelectionModel().getSelection();
            if (selection[0] != undefined) {
                var view = Ext.widget(this.editwidget);

                //TODO Verbeteren lelijke code?
                if (Ext.ComponentQuery.query('#fu1')[0] != undefined) {
                    Ext.ComponentQuery.query('#fu1')[0].store = this.getFunctieStore();
                    Ext.ComponentQuery.query('#fu2')[0].store = this.getFunctieStore();
                    Ext.ComponentQuery.query('#fu3')[0].store = this.getFunctieStore();
                    Ext.ComponentQuery.query('#fu4')[0].store = this.getFunctieStore();
                }
                if (Ext.ComponentQuery.query('#land')[0] != undefined) {
                    Ext.ComponentQuery.query('#land')[0].store = this.getLandenStore();
                }

                view.down('form').loadRecord(selection[0]);
                this.isWidgetOpen = true;
            } else {
                this.showMessageBox('Opmerking', 'Geen rij geselecteerd');
            }
        }
    },

    showAddWidget:function () {
        if (!this.isWidgetOpen) {
            var view = Ext.widget(this.addwidget);
            Ext.ComponentQuery.query('#fu1')[0].store = this.getFunctieStore();
            Ext.ComponentQuery.query('#fu2')[0].store = this.getFunctieStore();
            Ext.ComponentQuery.query('#fu3')[0].store = this.getFunctieStore();
            Ext.ComponentQuery.query('#fu4')[0].store = this.getFunctieStore();
            Ext.ComponentQuery.query('#land')[0].store = this.getLandenStore();
            this.isWidgetOpen = true;
        }
    },

    showRemoveUser:function () {
        var me = this;
        var list = Ext.ComponentQuery.query(this.list)[0];
        var selection = list.getSelectionModel().getSelection();
        if (selection.length != 0) {
            Ext.MessageBox.show({
                title:'Bevestiging verwijdering',
                msg:'Wilt u verwijderen?',
                buttons:Ext.MessageBox.YESNO,
                buttonText:{yes:'Ja', no:'Neen'},
                icon:Ext.MessageBox.WARNING,
                minWidth:400,
                maxWidth:600,
                fn:function (btn) {
                    if (btn == 'yes') {
                        me.removeUser(selection);
                    }
                }
            });
        } else {
            me.showMessageBox('Opmerking', 'Geen rij geselecteerd');
        }
    },

    setWidgetClosed:function () {
        this.isWidgetOpen = false;
    },

    teacherGridActive:function () {
        this.store = this.getBeheerLeraarStore();
        this.list = 'userlist';
        this.addwidget = 'useradd';
        this.editwidget = 'useredit';
    },

    studentGridActive:function () {
        this.store = this.getBeheerLeerlingStore();
        this.list = 'studentlist';
        this.addwidget = 'studentadd';
        this.editwidget = 'studentedit';
    },

    vakGridActive:function () {
        this.store = this.getVakStore();
        this.list = 'vaklist';
        this.addwidget = 'vakadd';
        this.editwidget = 'vakedit';
    }
});
